home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / educate / wordy402.zip / 1DIF.C next >
C/C++ Source or Header  |  1995-12-17  |  8KB  |  272 lines

  1. /**************************************************************************/
  2. /*                             1DIF Utility                               */
  3. /*                                                                        */
  4. /*                For use with 1DIF Scrab**e variant                      */
  5. /*                                                                        */
  6. /*                                 M\Cooper                               */
  7. /*                          3425 Chestnut Ridge Rd.                       */
  8. /*                        Grantsville, MD 21536-9801                      */
  9. /*                        --------------------------                      */
  10. /*                        Email:  thegrendel@aol.com                      */
  11. /*                                                                        */
  12. /*                $2.00 to register the entire WORDY package              */
  13. /*                                                                        */
  14. /**************************************************************************/
  15.  
  16. #include <conio.h>
  17. #include "srch.h"
  18.  
  19.  
  20. #define FILE_OPENING_ERROR 3
  21. #define FILENAME_MAXLEN 8
  22. #define CR "\n"
  23. #define FILE_SUFFIX ".1df"
  24. #define MAXLEN 40
  25. #define LINE_LEN 80
  26. #define MAXFILENAMELEN 40
  27. #define NOARGS 1
  28. #define INCREMENT 1
  29. #define SINGLE 1
  30. #define SPACE ' '
  31. #define WILDCARD '?'
  32. #define FILLCHAR '_'
  33.  
  34. #define BUFFERSIZE 8192
  35.  
  36.    char ad[] =
  37. "1DIF utility by M\\Cooper, 3425 Chestnut Ridge Rd., Grantsville, MD 21536-9801";
  38.  
  39. typedef enum { FALSE, TRUE } Boolean;
  40.  
  41. void getword( char *lset, int differences_allowed, char *file_name );
  42. void center( char *strng );
  43. Boolean wordtest( char *letterset, char *word, int differences );
  44.  
  45. /**************************************************************************/
  46.  
  47. void main( int argc, char **argv )
  48. {
  49.  
  50.    char letterset [MAXLEN],
  51.         filename [MAXFILENAMELEN];
  52.    int differences = 1; //Assume one (standard for 1DIF game),
  53.                         // unless specified otherwise.
  54.  
  55.       if( argc == NOARGS )
  56.          {
  57.          clrscr();
  58.          printf( "Enter a word to 1DIF ... " );
  59.          gets( letterset );
  60.          differences = 1;
  61.          strcpy( filename, Wordfile ); //Std. file
  62.          }
  63.       else
  64.          if( argc == NOARGS + 1 )
  65.            {
  66.            strcpy( letterset, *( argv + 1 ) );
  67.            strcpy( filename, Wordfile ); //Std. file
  68.            differences = 1;
  69.            }
  70.       else
  71.          if( argc == NOARGS + 2 )
  72.            {
  73.            strcpy( letterset, *( argv + 1 ) );
  74.            differences = atoi( *( argv + 2 ) );
  75.            strcpy( filename, Wordfile ); //Std. file
  76.            if( differences >= strlen( letterset ) || differences == 0 )
  77.                differences = 1;
  78.            }
  79.        else
  80.          {
  81.          strcpy( letterset, *( argv + 1 ) );
  82.          differences = atoi( *(argv + 2 ) );
  83.          strcpy( filename, *( argv + 3 ) );  //Specified file name.
  84.          if( differences >= strlen( letterset ) || differences == 0 )
  85.              differences = 1;
  86.          }
  87.  
  88.        getword( letterset, differences, filename );
  89. }
  90.  
  91.  
  92.  
  93. Boolean wordtest( char *letterset, char *word, int differences )
  94. {
  95.    char *dset;
  96.    register char *letpos;
  97.    register int diff;
  98.  
  99.       if( strlen( letterset) != strlen( word) )
  100.          return( FALSE );
  101.  
  102.       dset = letterset;
  103.       diff = 0;
  104.  
  105.       while( *word && *dset )
  106.          {
  107.          if( *dset == *word || *dset == WILDCARD )
  108.              ;  //As long as letter-match, just advance.
  109.          else
  110.              diff++;
  111.  
  112.              dset++;
  113.              word++;
  114.           }
  115.  
  116.       if( diff == differences ) /******DEBUG was <= ********/
  117.           return( TRUE ) ;
  118.       else
  119.           return( FALSE );
  120. }
  121.  
  122. /*************************************************************/
  123.  
  124. void getword( char *letter_set, int differences, char *filename )
  125. {
  126.  
  127.     char    l_set [ MAXLEN ],
  128.         word [ MAXLEN ],
  129.         tempstr [ MAXLEN + 1 ],
  130.         targetfile [ MAXLEN ],
  131.         bar [ LINE_LEN + 1 ],
  132.         double_bar [ LINE_LEN + 1 ],
  133.    msg2 [ MAXLEN ],
  134.    *ppos;
  135.  
  136.     FILE *fptr,
  137.         *tfile;
  138.     int fnamelen,
  139.      i;
  140.     long wcount = 0L;
  141.  
  142.        memset( bar, '-', LINE_LEN );
  143.        *( bar + LINE_LEN ) = NULL;
  144.        memset( double_bar, '=', LINE_LEN );
  145.        *( double_bar + LINE_LEN ) = NULL;
  146.  
  147.        /*************opening credits*************/
  148.        clrscr();
  149.        printf( double_bar );
  150.        strcpy( tempstr, ad );
  151.        center ( tempstr );
  152.        printf( tempstr );
  153.     printf( "\n" );
  154.        printf( double_bar );
  155.        printf( CR );
  156.        /****************************************/
  157.  
  158.  
  159.        strcpy ( l_set, letter_set );
  160.        strcat ( letter_set, CR );
  161.  
  162.        /*   Create name of file to store derived words in   */
  163.        /*********************************************************/
  164.        fnamelen = strlen( l_set );
  165.        if( fnamelen  > FILENAME_MAXLEN )
  166.           fnamelen = FILENAME_MAXLEN;
  167.        strncpy( targetfile, l_set, fnamelen );
  168.        *( targetfile + fnamelen ) = NULL;
  169.        //NULL-terminate string, so strcat works, ha, ha.
  170.  
  171.       for( i = 0; i < fnamelen; i++ )
  172.          if( *( targetfile + i ) == WILDCARD )
  173.              *( targetfile + i ) = FILLCHAR;
  174.  
  175.        strcat( targetfile, FILE_SUFFIX );
  176.        /*********************************************************/
  177.  
  178.        if( !( fptr = fopen( filename, "rt" ) ) )
  179.          {
  180.          printf( "\7\7\7Cannot open file %s!\n", filename );
  181.          exit( FILE_OPENING_ERROR );
  182.          }
  183.       if( setvbuf( fptr, NULL, _IOFBF, BUFFERSIZE * 2 ) )
  184.          exit ( FILE_OPENING_ERROR );  //Extra buffering.
  185.  
  186.        if( !( tfile = fopen( targetfile, "wt" ) ) )
  187.          {
  188.          printf( "\7\7\7Cannot open file to save words in!" );
  189.          exit ( FILE_OPENING_ERROR + 1 );
  190.          }
  191.       if( setvbuf( tfile, NULL, _IOFBF, BUFFERSIZE ) )
  192.          exit( FILE_OPENING_ERROR );  //Extra buffering.
  193.  
  194.  
  195.        /**************'Wait' Message************/
  196.        printf( CR CR );
  197.        printf( "WORKING...\n\n" );
  198.        printf( "This will a few seconds...\n" );
  199.        printf( "Please be patient.\n\n" );
  200.        printf(
  201.     "Now searching file %s and writing 1DIF file for %s\n\n",
  202.     filename, letter_set ); 
  203.  
  204.        /*****************************************/
  205.  
  206.  
  207.  
  208.  
  209.  
  210.        sprintf( tempstr, "Word(s) DIFFed from: %s\n", strupr( l_set ) );
  211.        center( tempstr );
  212.        fprintf( tfile, double_bar );
  213.       fprintf( tfile, CR );
  214.        fprintf( tfile, tempstr );
  215.        fprintf( tfile, double_bar );
  216.        fprintf( tfile, CR );
  217.  
  218.  
  219.          /*********************Main Loop*************/     
  220.           while( fgets( word, MAXLEN, fptr ) != NULL )
  221.  
  222.             if( wordtest( letter_set, word, differences ) )
  223.                {
  224.                fprintf( tfile, "%s", word );
  225.                wcount++;
  226.                }
  227.           /*******************************************/
  228.  
  229.       if( wcount == SINGLE )
  230.           strcpy( msg2, "word" );
  231.       else
  232.           strcpy( msg2, "words" );
  233.  
  234.           fprintf( tfile, bar );
  235.       fprintf( tfile, CR );
  236.           sprintf( tempstr, "%ld %s can be 1DIFFed from %s.",
  237.                  wcount, msg2, l_set );
  238.           center( tempstr );              
  239.           fprintf( tfile, tempstr );
  240.           fprintf( tfile, "\n\n" );
  241.  
  242.           center( ad );
  243.           fprintf( tfile, ad );
  244.  
  245.           fcloseall();
  246.  
  247.           sprintf( tempstr,
  248.                  "The file %s has %ld %s 1DIFFed from %s\7.",
  249.                  targetfile, wcount, msg2, l_set );
  250.           center( tempstr );
  251.           printf( CR CR );
  252.           printf( tempstr );
  253.     printf( CR );
  254.  
  255. }
  256.  
  257.  
  258.  
  259. void center( char *str )
  260. {
  261.    int padding;
  262.    char st [ LINE_LEN + INCREMENT ];
  263.  
  264.      padding = LINE_LEN / 2 - strlen( str ) / 2;
  265.      memset( st, SPACE, padding );
  266.      *( st + padding ) = NULL;  //Terminate string
  267.      strcat( st, str );
  268.      strcpy( str, st );
  269.  
  270.      return;
  271. }
  272.